
#include "CSettingsScreen.h"

void CSettingsScreen::load(CGraphics *gl)
{
    selection = 0;
    font = QFont("Verdana", 15, 0, 0);

    if (sm->getSound()->getSndEnabled()) sndState = 0;
    else sndState = 1;
    if (sm->getSound()->getMscEnabled()) mscState = 0;
    else mscState = 1;

    labels.append( TMLabel(QString::fromStdWString(L"Nustatymai"), 300, 120, font, -1 ) );
    labels.append( TMLabel(QString::fromStdWString(L"Garsai:"), 300, 150, font, -1 ) );
    labels.append( TMLabel(QString::fromStdWString(L"Muzika:"), 300, 180, font, -1 ) );
    buttons.append( TMLabel(sndStateTxt[sndState], 400, 150, font, 0 ) );
    buttons.append( TMLabel(mscStateTxt[mscState], 400, 180, font, 1 ) );
    buttons.append( TMLabel(QString::fromStdWString(L"Grįžti"), 50, 450, font, 2 ));

}

void CSettingsScreen::unload(CGraphics *gl)
{

}

int CSettingsScreen::update(CInput *in)
{
    int action = -1;

    if ( in->wasButtonPressed(Qt::LeftButton) )    // pelės paspaudimas
        foreach ( TMLabel l, buttons ) {
            int Mx = in->getCursorPos().x();
            int My = in->getCursorPos().y();
            if ( l.x < Mx && Mx < l.x2 && l.y2+10 < My && My < l.y+10 ) {
                action = l.action;
            }
        }

    if ( in->hasMouseMoved() )           // pelės žymėjimas
        foreach ( TMLabel l, buttons ) {
            int Mx = in->getCursorPos().x();
            int My = in->getCursorPos().y();
            if ( l.x < Mx && Mx < l.x2 && l.y2+10 < My && My < l.y+10 ) {
                if ( selection != l.action )
                    sm->getSound()->play( "audio/opr000L0-6.mp3", 0.5 );
                selection = l.action;
            }
        }

    if( ( in->wasKeyPressed(Qt::Key_Down) || in->wasKeyPressed(Qt::Key_Right) )
        && selection < buttons.size()-1 ) {  //  žymėjimas klaviatūra
        selection++;
        sm->getSound()->play( "audio/opr000L0-6.mp3", 0.5 );
    } else if( ( in->wasKeyPressed(Qt::Key_Up) || in->wasKeyPressed(Qt::Key_Left) )
        && selection > 0 ) {
        selection--;
        sm->getSound()->play( "audio/opr000L0-6.mp3", 0.5 );
    }

    if ( in->wasKeyPressed(Qt::Key_Return) )    // Enter
        action = selection;

    if ( in->wasKeyPressed(Qt::Key_Escape) )
        action = 2; // Grįžti į pagrindinį meniu

    if (action == 0) {
        sm->getSound()->switchSnd();
        if (sndState == 0) sndState = 1;
        else sndState = 0;
    }
    else if (action == 1) {
        sm->getSound()->switchMsc();
        if (mscState == 0) mscState = 1;
        else mscState = 0;
    }
    else if (action == 2) {
        sm->removeScreen(this);
    }

    buttons[0].text = sndStateTxt[sndState];
    buttons[1].text = mscStateTxt[mscState];

    for ( int i = 0; i < buttons.size(); i++ ) {
        if ( i == selection ) {
            buttons[i].fade += 0.2;
            if ( buttons[i].fade > 2.0 ) buttons[i].fade = 2.0;
        } else {
            if ( buttons[i].fade > 0.0 ) {
                buttons[i].fade -= 0.1;
                if ( buttons[i].fade < 0.0 ) buttons[i].fade = 0.0;
            }
        }
    }

    return 0;
}

void CSettingsScreen::draw(CGraphics *gl)
{
    glLoadIdentity();

    if ( sm->getScreen("backgroundScreen") == 0 ) { // nepiešti jei fone yra backgroundScreen
        glDisable(GL_TEXTURE_2D);       // piešiamas pusiau permatomas juodas ekranas
        glColor4f(0.0f, 0.0f, 0.0f, 0.5f );
        glBegin(GL_QUADS);  // fonas
            glVertex2i(  0, 0 );
            glVertex2i(  0, 500 );
            glVertex2i(700, 500 );
            glVertex2i(700, 0 );
        glEnd();
        glEnable(GL_TEXTURE_2D);
    }

    glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
    foreach ( TMLabel l, labels ) {
        gl->renderTexts( l.x, l.y, l.text, font );
    }
    foreach ( TMLabel l, buttons ) {
//        if ( selection == l.action ) glColor3f( 0.8f, 0.8f, 0.8f ); else glColor3f(1.0f, 1.0f, 1.0f );
        glColor4f( 1.0f, 1.0f, 1.0f, 0.5f+0.25*l.fade );
        gl->renderTexts( l.x, l.y, l.text, font );
    }
}
